<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends CommonAction{

	private $howtobig='';
	//主页面方法
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        Load('extend');
        $this->header_sibar();
        $product=M('Product');
        $pdata = $this->getProduct('is_popular=1','*',4,'sortall desc');
//        echo "<pre>";var_dump($pdata[1]);echo "</pre>";
        $this->assign('prodatas',$pdata);
        $hotArt = $this->getArticle('','*',6,'id');
//       echo "<pre>";var_dump($hotArt[1]);echo "</pre>";
        $this->assign('hotArt',$hotArt);
//        $hdata = $product->field('think_product.*,b.big_category_name as bname')->where('think_product.is_hot=1')->join('think_bigcategory b on b.id = think_product.pid')->limit(3)->select();
//        foreach($hdata as $k => $v){
//        	if($v['one_download_link'] == 0){
//        		$dl = M('ProductDownloadLink');
//        		$pdl = $dl->where('pid='.$v['id'])->select();
//        		$hdata[$k]['duoLink'] = $pdl;
//        		$p = $product->field('think_product.*')->where('think_product.id='.$v['relative_product'])->find();
//        		$hdata[$k]['relativePro'] = $p;
//        	}else{
//	        	$p = $product->field('think_product.*,b.big_category_name as bname')->join('think_bigcategory b on b.id = think_product.pid')->where('think_product.id='.$v['relative_product'])->find();
//	        	$hdata[$k]['relativePro'] = $p;
//        	}
//        }
//        $this->assign('hproduct',$hdata);
        $this->assign('flag',1);
		$this->assign('bluebg',1);
        $this->display();

    }

    private function getAll(){
     	$b = $this->getBig();
        foreach ($b as $k => $v){
        	$pcdata = $this->getArticleClassify('pid = '.$v['id'],'*',1,'sort asc');
        	$b[$k]['acdata'] = $pcdata[0];
        	$b[$k]['pdata'] = $this->getProduct('pid = '.$v['id'],'*','','psort asc');
        	$b[$k]['adata'] = $this->getArticle('pid = '.$v['id']);
        }
        echo "<pre>";var_dump($b[0]['pdata']);echo "</pre>";
        return $b;
    }
    //读取头和左侧数据
    private function header_sibar(){
    	//Load('extend');
    	$site_name = 'http://'.$_SERVER['HTTP_HOST'];
    	$this->assign('site_name',$site_name);
    	//查系统信息表，得到网站的name,keywork,discrition
    	$system = $this->getSystem();
    	$this->tkd($system['webname'],$system['web_keyword'],$system['web_discrition']);
        $this->assign('system',$system);
        //获得大类，好像在这没有用
    	//$cate=$this->getAll();
    	//$this->assign('cate',$cate);
    	$this->getFootLink();
    	//获得热门标签(没有用)
    	//$hotTag = $this->getHotTag();
	   	//$this->assign('hotTag',$hotTag);
	   	//获得所有产品
    	$allProduct=$this->getProduct('','*','','psort asc');
    	$this->assign('allProduct',$allProduct);
    	$allac = $this->getArticleClassify('','*','','id asc');
    	$this->assign('defaultAc',$allac[0]);


    }
    //查找是否有静态化的页面
    private function getFootLink(){
    	if(file_exists('./about-us.html')){
       		$this->assign('about_us',1);
       }
		if(file_exists('./contact-us.html')){
       		$this->assign('contact_us',1);
       }
		if(file_exists('./privacy-policy.html')){
       		$this->assign('privacy_policy',1);
       }
		if(file_exists('./terms-of-use.html')){
       		$this->assign('terms_of_use',1);
       }
    	if(file_exists('./resources.html')){
       		$this->assign('resources',1);
       }
    	if(file_exists('./sitemap.xml')){
       		$this->assign('sitemap',1);
       }
    	if(file_exists('./product.html')){
       		$this->assign('product_exist',1);
       }
        if(file_exists('./download.html')){
       		$this->assign('download_exist',1);
       }
	    if(file_exists('./support.html')){
       		$this->assign('support',1);
       }
       if(file_exists('./tutorials.html')){
       		$this->assign('tutorials',1);
       }
    }
    private function getSystem(){
    	$Msystem=M("System");
        $system=$Msystem->order('id desc')->find();
        return $system;
    }
    private function getArticle($where="",$field="*",$limit = '',$order="add_time"){
    	$article=M('Article');
//        $adata=$article->join('left join think_bigcategory b on b.id=think_article.pid left join think_article_classify c on c.id=think_article.cid')->order('add_time desc')->limit(12)->select();
 		$adata=$article->field($field)->order($order.' desc')->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getHotTag($where="",$field="*",$limit = '',$order="id desc"){
    	$ht=M('HotTag');
 		$adata=$ht->field($field)->order($order)->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getArticleClassify($where="",$field="*",$limit = '',$order="id desc"){
    	$ac=M('ArticleClassify');
 		$adata=$ac->field($field)->order($order)->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getProduct($where="",$field="*",$limit = '',$order="add_time desc"){
    	$p=M('Product');
 		$pdata=$p->field($field)->where($where)->order($order)->limit($limit)->select();
    	return $pdata;
    }
	private function getBig($where="",$field="*",$limit = '',$order="add_time"){
    	$b=M('Bigcategory');
 		$cate=$b->field($field)->where($where)->order($order.' asc')->limit($limit)->select();
		foreach($cate as $k=>$v){
    		if(file_exists("./".$v['big_category_name'].'-software.html')){
    			$cate[$k]['file_exit'] = 1;
    		}
    	}
    	return $cate;
    }
    //第一级分类点击进入
 	public function product(){
 		/*Load('extend');
        $this->header_sibar();
        $id=$_GET['id'];
        if(!$id){
        	return;
        }
        $bigcategory=M('Bigcategory');
        $bdata=$bigcategory->find($id);
        $this->assign('bdata',$bdata);
       $dl = M('ProductDownloadLink');
        $soft=$this->getProduct('pid='.$id,'*','','psort asc');
        foreach($soft as $k => $v){
        	$soft[$k]['introduce'] = stripcslashes($v['introduce']);
        	if($v['one_download_link'] == 0){
        		$pdl = $dl->where('pid='.$v['id'])->select();
        		$soft[$k]['duoLink'] = $pdl;
        	}
        }
        $this->assign('soft',$soft);
        $this->tkd($bdata['big_title'],$bdata['big_keyword'],$bdata['big_description']);

        $this->assign('pid',$id);
        $this->assign('sebg',2);*/
        $this->display();
    }
 //所有产品
 	public function allproduct(){
 		Load('extend');
        $this->header_sibar();
        $soft=$this->getProduct('','*','','psort asc');
        foreach($soft as $k => $v){
        	$soft[$k]['introduce'] = stripcslashes($v['introduce']);
        }
        $v_soft=$this->getProduct('hot_show=1','*','3','psort asc');
        $this->assign('vsoft',$v_soft);
        $this->assign('soft',$soft);
        //var_dump($soft[1]);
        $title = " Products Center of Recovery-Android.com";
        $keywords ="";
        $des = "";
        $this->tkd($title,$keywords,$des);
        $this->assign('bluebg',2);
        $this->display();
    }
//所有产品下载
 	public function alldownload(){
 		Load('extend');
        $this->header_sibar();
        $soft=$this->getProduct('','*','','psort asc');
        foreach($soft as $k => $v){
        	$soft[$k]['introduce'] = stripcslashes($v['introduce']);
        }
        $this->assign('soft',$soft);
        $title = "";
        $keywords ="";
        $des = "";
        $this->tkd($title,$keywords,$des);
        $this->display();
    }
//产品描述
   public function product_description(){
 		Load('extend');
   		$this->header_sibar();
   		$id=$_GET['id'];
        if(!$id){
        	return;
        }
        $product = M('Product');
        $cate = M('Bigcategory');
        $soft=$product->find($id);
        $soft['introduce']=stripcslashes($soft['introduce']);
        $c_data = $cate->field('big_category_name,id')->find($soft['pid']);
        $soft['bname'] = $c_data['big_category_name'];
        $soft['bigid'] = $c_data['id'];
   		if(file_exists("./".$soft['bname'].'-software.html')){
    		$soft['file_exit'] = 1;
    	}
   		if($soft['one_download_link'] == 0){
        	$dl = M('ProductDownloadLink');
        	$pdl = $dl->where('pid='.$soft['id'])->select();
        	$soft['duoLink'] = $pdl;
        }
        $this->assign('soft',$soft);
        $style="style='background-color:#fff'";
        $this->assign('style',$style);
        //var_dump($soft);
        $this->tkd($soft['product_title'],$soft['product_keyword'],$soft['introduce']);

   	 	$this->display();
   }

   //产品指南
   public function guide(){
 		Load('extend');
   		$this->header_sibar();
   		$id=$_GET['id'];
        if(!$id){
        	return;
        }
        $product = M('Product');
        $cate = M('Bigcategory');
        $soft=$product->find($id);
        $soft['introduce']=stripcslashes($soft['introduce']);
        $c_data = $cate->field('big_category_name,id')->find($soft['pid']);
        $soft['bname'] = $c_data['big_category_name'];
        $soft['bigid'] = $c_data['id'];
   		if(file_exists("./".$soft['bname'].'-software.html')){
    		$soft['file_exit'] = 1;
    	}
   		if($soft['one_download_link'] == 0){
        	$dl = M('ProductDownloadLink');
        	$pdl = $dl->where('pid='.$soft['id'])->select();
        	$soft['duoLink'] = $pdl;
        }
        $this->assign('soft',$soft);
        $style="style='background-color:#fff'";
        $this->assign('style',$style);
        //var_dump($soft);
        $this->tkd($soft['product_title'],$soft['product_keyword'],$soft['introduce']);

   	 	$this->display();
   }
   //技术说明
   public function tech_spec(){
 		$this->product_description();

   }
   
   public function reviews(){
	   	Load('extend');
	   	$this->header_sibar();
   		$id=$_GET['id'];
   		if(!$id){
   			return;
   		}
   		$reviews=M('Reviews');
   		$soft['id']=$id;
   		$pro_reviews=$reviews->where('pid='.$id)->select();
	   	$style="style='background-color:#fff'";
	   	$this->assign('data',$pro_reviews);
	   	$this->assign('style',$style);
	   	$this->assign('soft',$soft);
	   	$this->display();
   }
//	public function classify(){
//		Load('extend');
//        $this->header_sibar();
//		$id=$_GET['id'];
//        if(!$id){
//        	return;
//        }
//        $product=M('Product');
//
//        $soft=$product->where('pid='.$id)->order( 'add_time')->select();
//        $this->assign('soft',$soft);
//
//        $plate=M('Plate');
//        $pdata=$plate->find($id);
//        $this->assign('second_name',$pdata['plate_name']);
//        $this->assign('keyword',$pdata['plate_keyword']);
//        if($pdata['plate_title']){
//        	$this->assign('title',$pdata['plate_title']);
//        }
//        $cate=M('Bigcategory');
//        $cate=$cate->find($pdata['pid']);
//        $this->assign('first_name',$cate['big_category_name']);
//        $this->assign('description',$this->html($pdata['plate_description']));
//        $this->display();
//    }
	public function article(){
		Load('extend');
        $this->header_sibar();
        $id=$_GET['id'];
        if(!$id){
        	return;
        }
        $article=M('Article');
        $ac = M('ArticleClassify');
        $adata=$article->find($id);
        $this->tkd($adata['web_title'],$adata['keyword'],$adata['introduce']);

        $cate = M('Bigcategory');
        $c_data = $cate->field('big_category_name,id')->find($adata['pid']);
        $acMsg = $ac->field('id,classify_name,statice_name as acsn,is_statice as acis,new_art_img,robot')->where('id='.$adata['cid'])->find();
        $adata['bname'] = $c_data['big_category_name'];
        $adata['bigid'] = $c_data['id'];
        $adata['acid'] = $acMsg['id'];
        $adata['acsn'] = $acMsg['acsn'];
        $adata['classify_name'] = $acMsg['classify_name'];
        $adata['acis'] = $acMsg['acis'];
        $adata['new_art_img'] = $acMsg['new_art_img'];
        $adata['robot'] = $acMsg['robot'];
		if(file_exists("./".$adata['bname'].'-software.html')){
    		$adata['file_exit'] = 1;
    	}
        $this->assign('adata',$adata);
//        if(!empty($adata['search_word'])){
//        	$relArt = $article->where("title like '%".$adata['search_word']."%' and pid=".$adata['pid'])->select();
//        }else{
//        	$relArt = $article->where("pid=".$adata['pid'])->select();
//        }
//        $this->assign('relArt',$relArt);
		$art = $this->getArticle('is_hot=1 and cid='.$adata['cid'],'*','10','sort');
    	$this->assign('art',$art);
    	$style="style='background-color:#fff'";
    	$this->assign('style',$style);
        $this->display();
    }
	public function howto(){
		Load('extend');
		$this->header_sibar();
//        $b=M('Bigcategory');
        $a = M('Article');
		$ac = M('ArticleClassify');
        $allacdata = $ac->order('id asc')->select();
		$acid=$_GET['id'];
//		$pid = $_GET['pid'];
        if(!$acid){
        	$msg = $allacdata[0];
        }else{
        	$msg = $ac->find($acid);
        }
//        $this->assign('sebg',1);
//        $this->assign('pid',$pid);
		$adata = $a->where('cid='.$msg['id'])->order('sort asc')->select();
//		$msg = $ac->find($acid);
		$this->tkd($msg['seo_title'],$msg['seo_keywords'],$msg['seo_description']);
		$this->assign('allacdata',$allacdata);
		$this->assign('msg',$msg);
		$this->assign('adata',$adata);
//		$bMsg =  $b->find($pid);
		//$hdata = $product->field('think_product.*,b.big_category_name as bname')->where('think_product.is_hot=1')->join('think_bigcategory b on b.id = think_product.pid')->limit(2)->select();
//		if($msg['pro_id']){
//			$pMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['pro_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
//			$pMsg = $pMsg[0];
//			if($pMsg['one_download_link'] == 0){
//	        	$pdl = $dl->where('pid='.$pMsg['id'])->select();
//	        	$pMsg['duoLink'] = $pdl;
//			}
//	    }else{
//	    	$pMsg = '';
//	    }
//        if($msg['psec_id']){
//			$relativeMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['psec_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
//	        $relativeMsg = $relativeMsg[0];
//		 	if($relativeMsg['one_download_link'] == 0){
//	        	$pdl = $dl->where('pid='.$relativeMsg['id'])->select();
//	        	$relativeMsg['duoLink'] = $pdl;
//	        }
//        }else{
//        	$relativeMsg = '';
//        }
////		foreach($acdata as $k => $v){
//			$awdata = $a->where('cid='.$acid.' and pid='.$pid.' and pc=1')->select();
//			$amdata = $a->where('cid='.$acid.' and pid='.$pid.' and pc=2')->select();
////		}
//		$this->assign('bMsg',$bMsg);
//		$this->assign('rMsg',$relativeMsg);
//		$this->assign('pMsg',$pMsg);
//		$this->assign('sMsg',$msg);
//        $this->assign('acMsg',$acdata);
//        $this->assign('awMsg',$awdata);
//         $this->assign('amMsg',$amdata);
//        $this->assign('acid',$acid);
       $this->display();
    }

	public function tutorials(){
		Load('extend');
		$this->header_sibar();
//        $b=M('Bigcategory');
        $a = M('Article');
		$ac = M('ArticleClassify');
		//获得所有类
        $allacdata = $ac->order('id asc')->select();
		$acid=$_GET['id'];
//		$pid = $_GET['pid'];
        if(!$acid){
        	$msg = $allacdata[0];
        }else{
        	$msg = $ac->find($acid);
        
        }
//        $this->assign('sebg',1);
//        $this->assign('pid',$pid);
		$adata = $a->where('cid='.$msg['id'])->order('sort asc')->select();
		//var_dump($adata);
		$exadata=$a->where('is_new=1 and cid='.$msg['id'])->limit(3)->select();
		$this->assign('exd',$exadata);
		//var_dump($exadata);
//		$msg = $ac->find($acid);
		$this->tkd($msg['seo_title'],$msg['seo_keywords'],$msg['seo_description']);
		$this->assign('allacdata',$allacdata);
		$this->assign('msg',$msg);
		$this->assign('adata',$adata);
//		$bMsg =  $b->find($pid);
		//$hdata = $product->field('think_product.*,b.big_category_name as bname')->where('think_product.is_hot=1')->join('think_bigcategory b on b.id = think_product.pid')->limit(2)->select();
//		if($msg['pro_id']){
//			$pMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['pro_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
//			$pMsg = $pMsg[0];
//			if($pMsg['one_download_link'] == 0){
//	        	$pdl = $dl->where('pid='.$pMsg['id'])->select();
//	        	$pMsg['duoLink'] = $pdl;
//			}
//	    }else{
//	    	$pMsg = '';
//	    }
//        if($msg['psec_id']){
//			$relativeMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['psec_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
//	        $relativeMsg = $relativeMsg[0];
//		 	if($relativeMsg['one_download_link'] == 0){
//	        	$pdl = $dl->where('pid='.$relativeMsg['id'])->select();
//	        	$relativeMsg['duoLink'] = $pdl;
//	        }
//        }else{
//        	$relativeMsg = '';
//        }
////		foreach($acdata as $k => $v){
//			$awdata = $a->where('cid='.$acid.' and pid='.$pid.' and pc=1')->select();
//			$amdata = $a->where('cid='.$acid.' and pid='.$pid.' and pc=2')->select();
////		}
//		$this->assign('bMsg',$bMsg);
//		$this->assign('rMsg',$relativeMsg);
//		$this->assign('pMsg',$pMsg);
//		$this->assign('sMsg',$msg);
//        $this->assign('acMsg',$acdata);
//        $this->assign('awMsg',$awdata);
//         $this->assign('amMsg',$amdata);
       $this->assign('acid',$acid);
       $this->display();
    }

    private function tkd($title='',$keyword='',$des=''){
    	if($title){
    		$this->assign('title',$title);
    	}
    	if($keyword){
    		$this->assign('keyword',$keyword);
    	}
    	if($des){
    		$this->assign('description',$this->html($des));
    	}
    }
   //过滤html标签
	private function html($str){
		$pattern = "/(<strong>|<\/strong>|<br>|<br \/>)/i";
		return preg_replace($pattern,"",$str);
	}
	public function buy_num(){
		$id = $_POST['id'];
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('buy_num',$pdata['buy_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'buy_num'=>1,'time'=>$date))->add();
		}
//		$pdata = $product->field('buy_link,buy_num')->find($id);
//		$product->where('id='.$id)->setField('buy_num',$pdata['buy_num']+1);
//		$this->assign('buy_link',$pdata['buy_link']);
//		header("Location:".$pdata['buy_link']);
//		$this->display();
	}
	public function downloadNum(){
		$id = $_POST['id'];
		if(empty($id)){
			return false;
		}
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();
		}echo 1;exit;
//		$pdata = $product->field('download_link,download_num')->find($id);
//		header("Location:".$pdata['download_link']);

	}

	public function download()
	{
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$pdl = M('ProductDownloadLink');
		$p = $pdl->where('id='.$id)->find();
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$p['pid'].' and time="'.$date.'"')->find()){
			$dy->where('pid='.$p['pid'].' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$p['pid'],'down_num'=>1,'time'=>$date))->add();
		}
//		$pdata = $product->field('download_link,download_num')->find($p['pid']);
		header("Location:".$p['url']);
	}

	public function buy()
	{
		$id = $_GET['id'];
		if(empty($id))
		{
			return;
		}
		$product = M('Product');
		$pdata = $product->find($id);
		$this->assign('product',$pdata);
		$this->display();
	}
 	public function aboutUs(){
       $this->common();
       $this->assign('title','About MyJad Studio');
       $this->assign('keyword','about us, more about myjad studio');
       $this->assign('description','This article help you learn more about MyJad Studio.');
       $this->display();
    }
	public function support(){
       $this->common();
       $v_soft=$this->getProduct('hot_show=1','*','3','psort asc');
       $this->assign('vsoft',$v_soft);
       $this->assign('title','Recovery-Android Studio Support Center');
       $this->assign('keyword','support center, software support');
       $this->assign('description','Support Center of Recovery-Android Studio Software.');
       $this->display();
    }
	public function contactUs(){
       $this->common();
       $this->assign('title','Contacts us - Recovery-Android.com');
       $this->assign('keyword','contact us, contact');
       $this->assign('description',"Contact Recovery-Android.com for Android Transfer and Recovery Data Sofware.");
       $this->display();
    }
	public function privacyPolicy(){
       $this->common();
        $this->assign('title','Privacy Policy of Recovery-Android.com');
       $this->assign('keyword','recovery-android privacy policy, privacy policy of recovery-android studio');
       $this->assign('description','Recovery-Android Studio fully respects your right to privacy, and will not collect any personal information about you without your clear permission.');
       $this->display();
    }
	public function termsOfUse(){
       $this->common();
        $this->assign('title','Terms & Conditions of Recovery-Android Studio');
       $this->assign('keyword','recovery-android terms and conditions, recovery-android studio terms of use');
       $this->assign('description','More about Recovery-Android Studio Terms & Conditions.');
       $this->display();
    }
	public function resources(){
       $this->common();
        $this->assign('title','Links to recovery-andorid Studio');
       $this->assign('keyword','links, link to us, link to recovery-android studio');
       $this->assign('description','Links to Recovery-Android Studio Website.');
       $this->display();
    }
    private function common()
    {
    	header("Content-Type:text/html; charset=utf-8");
        Load('extend');
        $this->header_sibar();
        $product=M('Product');
        //是否流行
        $pdata=$product->where('is_popular=1')->limit(4)->select();
        $this->assign('product',$pdata);
        $hdata=$product->where('is_hot=1')->limit(2)->select();
        $this->assign('hproduct',$hdata);
        $newsoft=$product->order('add_time desc')->limit(12)->select();
        $this->assign('newsoft',$newsoft);
        $this->assign('flag',1);

    }
	public function purchasing(){
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('buy_num',$pdata['buy_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'buy_num'=>1,'time'=>$date))->add();
		}
		$pdata = $product->field('buy_link,buy_num')->find($id);
//		$product->where('id='.$id)->setField('buy_num',$pdata['buy_num']+1);
//		$this->assign('buy_link',$pdata['buy_link']);
		header("Location:".$pdata['buy_link']);
//		$this->display();
	}
	public function downloading(){
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();
		}
		$pdata = $product->field('download_link,download_num')->find($id);
		header("Location:".$pdata['download_link']);
//		$product->where('id='.$id)->setField('download_num',$pdata['download_num']+1);
//		echo $pdata['download_link'];

	}

	public function search()
	{
		Load('extend');
        $this->header_sibar();
		$keywords=$_GET['tag'];
        if(!$keywords){
        	return;
        }
        $article=M('Article');
        import("ORG.Util.Page"); //  导入分页类
		$count = $article->where("title like '%".$keywords."%'")->count(); //  查询满足要求的总记录数
        $Page = new Page($count,10);

         $Page->setConfig('header', 'records');
        $Page->setConfig('prev', '< prev');
        $Page->setConfig('next', 'next >');
        $Page->setConfig('first', 'first');
        $Page->setConfig('last', 'last');
//        $Page->setConfig('theme', '%totalRow% %header% %nowPage%/%totalPage% pages %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
        $Page->setConfig('theme', '%totalRow% %header% %nowPage%/%totalPage% pages %upPage% %first% %linkPage% %downPage% %end%');
        $show  = $Page->show(); //  分页显示输出

        $soft=$article->where("title like '%".$keywords."%'")->order( 'add_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('keywords',$keywords);
        $this->assign('article',$soft);
        $this->assign('page',$show); //  赋值分页输出

        $this->display();
	}

	public function search_result()
	{
        $this->header_sibar();

        $this->display();
	}

	//统计产品的下载次数
	public function tongji(){
		$id = $_POST['id'];
		$url= $_POST['url'];
		if(empty($id)){
			return false;
		}
		$product = M('product');
		$dy = M('download_buy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);

		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();

		}

	}
	public function buytongji(){
		$id = $_POST['id'];
		if(empty($id)){
			return false;
		}
		$product = M('product');
		$dy = M('download_buy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('buy_num',$pdata['buy_num']+1);

		}else{
			$dy->data(array('pid'=>$id,'buy_num'=>1,'time'=>$date))->add();
		}


	}

	//404页面
	public function show404(){
		Load('extend');
		$this->header_sibar();
		$title = 'Page Not Found | Recovery-Android';
		$keyword='page not found, 404 error, error page';
		$description='We are sorry, but the page you are finding was doesn\'t exist.';
		$this->tkd($title,$keyword,$description);

		$this->display();
	}


}
?>